package com.starsea.book.ui.newpage.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.constant.PreferKey
import com.starsea.book.databinding.ActivityCodeSignBinding
import com.starsea.book.databinding.ActivityRegisterBinding
import com.starsea.book.ui.main.MainActivity
import com.starsea.book.ui.newpage.LoginActivity
import com.starsea.book.ui.newpage.utils.AppManager
import com.starsea.book.ui.newpage.utils.CountTimeState
import com.starsea.book.ui.newpage.viewmodel.LoginViewModel
import com.starsea.book.ui.widget.VerifyEditText
import com.starsea.book.utils.EncryptUtil
import com.starsea.book.utils.putPrefBoolean
import com.starsea.book.utils.putPrefString
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding


/**
 * @author Lee
 * @createTime 2024-07-11 21 GMT+8
 * @desc :
 */
class CodeSignActivity : VMBaseActivity<ActivityCodeSignBinding, LoginViewModel>() {

    /**
     *  0 登录
     *  1 忘记密码
     *  2 注册
     */
    var mType = 0

    var phone = ""
    var pwd = ""
    var recomendCode = ""

    override val viewModel: LoginViewModel by viewModels<LoginViewModel>()

    override val binding: ActivityCodeSignBinding by viewBinding(ActivityCodeSignBinding::inflate)

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        binding.titleBarView.setOnLeftTextClickListener {
            finish()
        }

        mType = intent.getIntExtra("mType", 0)
        phone = intent.getStringExtra("phone") ?: ""

        if (mType == 2) {
            pwd = intent.getStringExtra("pwd") ?: ""
            recomendCode = intent.getStringExtra("recomendCode") ?: ""
        }

        binding.tvHintPhone.text = "验证码已发送至$phone"

        viewModel.count()
        binding.rtvReCode.isEnabled = false
        binding.rtvReCode.setOnClickListener {
            viewModel.getCode(phone)
        }

        binding.vetCode.addInputCompleteListener(object : VerifyEditText.InputCompleteListener {
            override fun complete(content: String) {

                when (mType) {
                    0 -> {
//            MainActivity.start(mContext)
//                        viewModel.login(phone, "", content, 2, this@CodeSignActivity)
                    }

                    1 -> {
//                        startActivity<ForgetChangePwdActivity>()
                    }

                    2 -> {
                        LogUtils.e(">>> 未加密$pwd")
                        LogUtils.e(">>> 加密${EncryptUtil.encrypt(pwd)}")

                        viewModel.register(
                            phone,
                            EncryptUtil.encrypt(pwd),
                            content,
                            recomendCode, this@CodeSignActivity
                        )
                    }
                }
            }
        })



        viewModel.registerResutl.observe(this) {
            when (it.status) {
                200 -> {
                    LogUtils.e(">>>>> " + it.data)
                    toastOnUi("注册成功")
                    putPrefBoolean(PreferKey.isLogin, true)
                    putPrefString(PreferKey.token, it.data ?: "")
                    startActivity<MainActivity>()
                    AppManager.getInstance().killActivity(LoginActivity::class.java)
                    AppManager.getInstance().killActivity(RegisterActivity::class.java)
                    finish()
                }

                else -> {
//          ToastUtils.showShort(it.error.errorMsg)
                }

            }
        }
        viewModel.loginResutl.observe(this) {
            when (it.status) {
                200 -> {
//                    CacheUtil.setToken(it.data)
                    toastOnUi("登录成功")
                    LogUtils.e(">>>>> " + it.data)
                    putPrefBoolean(PreferKey.isLogin, true)
                    putPrefString(PreferKey.token, it.data ?: "")
                    startActivity<MainActivity>()
                    AppManager.getInstance().killActivity(LoginActivity::class.java)
                    finish()
                }

                else -> {
//          ToastUtils.showShort(it.error.errorMsg)
                }


            }
        }

        viewModel.countTime.observe(this) {
            when (it) {
                is CountTimeState.Finish -> {
                    binding.rtvReCode.isEnabled = true
                    binding.rtvReCode.text = "重新发送"
                }

                is CountTimeState.Loading -> {
                    binding.rtvReCode.isEnabled = false
                    binding.rtvReCode.text = "${it.data}s后重新发送"
                }
            }
        }


        viewModel.codeResult.observe(this) {
            when (200) {
                200 -> {
                    ToastUtils.showShort("短信发送成功")
                    viewModel.count()
                    binding.rtvReCode.isEnabled = false
                }

                else -> {
//                    ToastUtils.showShort(it.error.errorMsg)
//          LogUtils.e(">>>> ${it.error.errorMsg}")
//          LogUtils.e(">>>> ${it.error.errCode}")
                }


            }
        }

        viewModel.errorResult.observe(this) {
            ToastUtils.showShort(it ?: "")
        }

    }


}